// -----------------------------------------------------------------------
//  <copyright file="RandomExtensions.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2025 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025-03-07 18:03</last-date>
// -----------------------------------------------------------------------

namespace DaprPlus.Extensions;

public static class RandomExtensions
{
    private static readonly Random _random = new();

    public static T? NextItem<T>(this IEnumerable<T> items)
    {
        if (items == null)
        {
            throw new ArgumentNullException(nameof(items));
        }

        var list = items.ToList();
        if (list.Count == 0)
        {
            return default;
        }

        var index = _random.Next(0, list.Count);
        return list[index];
    }
}
